<?php
class DirWalker {
	private $nodes;
	private $hostname;
	private $dir;
	
	public function __construct($localDir) {
		$this->hostname = gethostname();
		if (!is_dir($localDir)) {
			throw new Exception ("$localDir is not a directory");
		}
		$this->dir = $localDir;
		$this->__walk($this->dir);
	}
	
	private function __walk($dir) {
		if (!is_dir($dir)) {
			throw new Exception ("$dir is not a directory");
		}
		$d = dir($dir);
		while (false !== ($entry = $d->read())) {
			if ($entry == "." || $entry == "..") {
				continue;
			}	
		
			$fqpn = "$dir/$entry";
			if (is_dir($fqpn)) {
				$this->__walk($fqpn);
			}
			if (is_file($fqpn)) {
				$this->nodes[$fqpn] = stat($fqpn);
				$this->nodes[$fqpn]["hmac"] = hash_hmac("sha256", $fqpn, $this->hostname . "|$fqpn");
			}
		}
	}
	
	public function rewalk() {
		$this->__walk($this->dir);
	}
	
	public function walk() {
		return $this->nodes;
	}
}
?>